package cn.zhangjie.basic.controller;

import cn.zhangjie.basic.exception.BusinssException;
import cn.zhangjie.basic.service.IVerifyCodeService;
import cn.zhangjie.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//图形验证接口
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService iVerifyCodeService;

    @GetMapping("/image/{key}")
    public JsonResult getverifyCode(@PathVariable("key") String key){
        try {
            String baseStr = iVerifyCodeService.getVerifyCode(key);
            return JsonResult.me().setResultObj(baseStr);
        }catch (BusinssException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("获取图形失败，稍后重试！");
        }
    }

    @PostMapping("/smsCode")
    public JsonResult smsCode(@RequestBody Map<String, String> map){
        try {
            iVerifyCodeService.smsCode(map);
            return JsonResult.me();
        }catch (BusinssException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常，稍后重试");
        }
    }

    //绑定验证码
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String, String> map){
        try {
            iVerifyCodeService.binderSmsCode(map);
            return JsonResult.me();
        }catch (BusinssException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常，稍后重试");
        }
    }

}
